package com.lib.common.view.explosionview;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class ExplosionView extends View {
    private List<ExplosionAnimator> mExplosions = new ArrayList<>();
    private int[] mExpandInset = new int[2];

    public ExplosionView(Context context) {
        super(context);
        init();
    }

    public ExplosionView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ExplosionView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {

        Arrays.fill(mExpandInset, Utils.dp2Px(32));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (ExplosionAnimator explosion : mExplosions) {
            explosion.draw(canvas);
        }
    }

    /**
     * 调用此方法 将爆炸视图粘附在窗口上
     *
     * @param activity 当前Activity
     * @return
     */
    public static ExplosionView attachToWindow(Activity activity) {
        ViewGroup rootView = (ViewGroup) activity.findViewById(Window.ID_ANDROID_CONTENT);
        ExplosionView view = new ExplosionView(activity);
        rootView.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        return view;
    }

    /**
     * '
     * 设置爆炸的范围
     *
     * @param dx
     * @param dy
     */
    public void setExplosionBound(int dx, int dy) {

        mExpandInset[0] = dx;
        mExpandInset[1] = dy;
    }

    /**
     * 调用此方法 展现爆炸效果
     *
     * @param view 对应的控件
     */
    public void explode(final View view) {

        final int width = view.getWidth();
        final int height = view.getHeight();
        final Random random = new Random();
        view.setOnClickListener(null);

        Rect r = new Rect();
        view.getGlobalVisibleRect(r);
        int[] location = new int[2];
        getLocationOnScreen(location);
        r.offset(-location[0], -location[1]);
        r.inset(-mExpandInset[0], -mExpandInset[1]);
        ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f).setDuration(150);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {

                float nextFloat = random.nextFloat();
                view.setTranslationX((nextFloat - 0.5f) * width * 0.05f);
                view.setTranslationY((nextFloat - 0.5f) * height * 0.05f);
            }
        });
        animator.start();
        int startDelay = 100;
        view.animate().setDuration(150).setStartDelay(startDelay).scaleX(0f).scaleY(0f).alpha(0f).start();
        explode(Utils.createBitmapFromView(view), r, startDelay, ExplosionAnimator.DEFAULT_DURATION);
    }

    public void explode(Bitmap bitmap, Rect bound, long startDelay, long duration) {

        final ExplosionAnimator explosion = new ExplosionAnimator(this, bitmap, bound);
        explosion.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationEnd(Animator animation) {

                mExplosions.remove(animation);
            }
        });
        explosion.setStartDelay(startDelay);
        explosion.setDuration(duration);
        mExplosions.add(explosion);
        explosion.start();
    }

    public void clear() {

        mExplosions.clear();
        invalidate();
    }

    public void reset(View view) {

        view.setScaleX(1);
        view.setScaleY(1);
        view.setAlpha(1);
    }
}
